package com.scqkxx.gamebox.activity.myself;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.view.TitleView;
import com.scqkxx.gamebox.activity.myself.address.AddressPicker;
import com.scqkxx.gamebox.databinding.ActivityMyselfEditAddressBinding;
import com.scqkxx.gamebox.databinding.ActivityMyselfEditSignatureBinding;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.library.utils.ToastUtils;

import java.util.List;

/**
 * 修改地址
 * Created by hef on 2018/8/13.
 */

public class MyselfEditAddressActivity extends BaseActivity<ActivityMyselfEditAddressBinding> {

    private AVObject mAVObject;//保存地址对象，若能在地址表中查到，则用查到的对象，不能查到则new一个

    @Override
    public int setLayoutId() {
        return R.layout.activity_myself_edit_address;
    }

    @Override
    public void init() {
        binding.tvTitleView.setOnTitleClickListener(new TitleView.onTitleClickListener() {
            @Override
            public void onTitleClick(int mTitleType) {
                if (TitleView.CLICK_BACK == mTitleType) {
                    finish();
                }
            }
        });

        AddressPicker addressPicker = new AddressPicker();
        addressPicker.setOnConfirmListener(new AddressPicker.OnConfirmListener() {
            @Override
            public void onConfirm(String address) {

            }
        });

        binding.layoutLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addressPicker.selectAddressDialog(MyselfEditAddressActivity.this, binding.tvLocation);
            }
        });

        binding.btSaveAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = binding.evName.getText().toString();
                String phone = binding.evPhone.getText().toString();
                String location = binding.tvLocation.getText().toString();
                String addressDetail = binding.evAddressDetail.getText().toString();

                if(TextUtils.isEmpty(name)){
                    ToastUtils.showShortToast(MyselfEditAddressActivity.this,"请输入姓名");
                    return;
                }

                if(TextUtils.isEmpty(phone)){
                    ToastUtils.showShortToast(MyselfEditAddressActivity.this,"请输入手机号码");
                    return;
                }

                if(phone.length() != 11){
                    ToastUtils.showShortToast(MyselfEditAddressActivity.this,"请输入正确的手机号码");
                    return;
                }

                if(TextUtils.isEmpty(location)){
                    ToastUtils.showShortToast(MyselfEditAddressActivity.this,"请输入所在地区");
                    return;
                }

                if(TextUtils.isEmpty(addressDetail)){
                    ToastUtils.showShortToast(MyselfEditAddressActivity.this,"请输入详细地址");
                    return;
                }

                saveAddress(name,phone,location,addressDetail);
            }
        });

        getAddress();
    }

    /**
     * 获取地址
     */
    private void getAddress(){
        AVUser avUser = AVUser.getCurrentUser();
        if(avUser == null) return;
        AVQuery<AVObject> query = AVQuery.getQuery("A_DxtAddress");
        query.whereEqualTo("userId",avUser.get("id"));
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(e == null){
                    if(list != null && list.size() > 0){
                        AVObject avObject = list.get(0);
                        mAVObject = avObject;
                        binding.evName.setText(avObject.getString("name"));
                        binding.evPhone.setText(avObject.getString("mobilePhoneNumber"));
                        binding.tvLocation.setText(avObject.getString("area"));
                        binding.evAddressDetail.setText(avObject.getString("address"));
                    }else {
                        mAVObject = new AVObject("A_DxtAddress");
                    }
                }
            }
        });
    }

    /**
     * 保存地址
     * @param name
     * @param phone
     * @param location
     * @param addressDetail
     */
    private void saveAddress(String name,String phone,String location,String addressDetail){
        if(mAVObject == null) return;
        AVUser avUser = AVUser.getCurrentUser();
        if(avUser == null){
            startActivity(new Intent(this,LoginActivity.class));
            return;
        }

        mAVObject.put("name",name);
        mAVObject.put("mobilePhoneNumber",phone);
        mAVObject.put("area",location);
        mAVObject.put("address",addressDetail);
        mAVObject.put("userId",avUser.get("id"));
        mAVObject.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if(e == null){
                    ToastUtils.showShortToast(MyselfEditAddressActivity.this,"保存成功");
                    finish();
                }
            }
        });
    }
}
